package com.example.boot.dlgoods.service.impl;

import com.example.boot.dlgoods.dao.DlGoodsMapper;
import com.example.boot.dlgoods.model.DlGoods;
import com.example.boot.dlgoods.service.DlGoodsService;
import com.example.boot.dlgoods.vo.GoodsDetail;
import com.example.boot.dlgoodspic.dao.DlGoodsPicMapper;
import com.example.boot.dlgoodspic.model.DlGoodsPic;
import com.example.boot.dlgoodsss.dao.DlGoodsSsMapper;
import com.example.boot.dlgoodsss.model.DlGoodsSs;
import com.example.boot.dlgoodsss.vo.GoodsSsGg;
import com.example.boot.dlorder.dao.DlOrderMapper;
import com.example.boot.dlorderevaluate.dao.DlOrderEvaluateMapper;
import com.example.boot.dlorderevaluate.model.DlOrderEvaluate;
import com.example.boot.dlshop.dao.DlShopMapper;
import com.example.boot.dlshop.model.DlShop;
import com.example.boot.dlshopclause.dao.DlShopClauseMapper;
import com.example.boot.dlshopclause.model.DlShopClause;
import com.example.boot.tstype.dao.TSTypeMapper;
import com.example.boot.util.NumberDictionary;
import com.example.boot.util.PicURLEntity;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DlGoodsImpl implements DlGoodsService {
    @Autowired
    private DlGoodsMapper dlGoodsMapper;
    @Autowired
    private DlOrderEvaluateMapper dlOrderEvaluateMapper;
    @Autowired
    private DlShopMapper dlShopMapper;
    @Autowired
    private DlGoodsSsMapper dlGoodsSsMapper;
    @Autowired
    private DlOrderMapper dlOrderMapper;
    @Autowired
    private DlGoodsPicMapper dlGoodsPicMapper;
    @Autowired
    private PicURLEntity picURLEntity;
    @Autowired
    private DlShopClauseMapper dlShopClauseMapper;
    @Autowired
    private NumberDictionary numberDictionary;
    @Autowired
    private TSTypeMapper tsTypeMapper;

    @Override
    public List<DlGoods> pagingGoods(Integer page, Integer size) {
        if (page > 0) {
            page = (page - 1) * 10;
        }
        List<DlGoods> findByIndexGoods = dlGoodsMapper.findByIndexGoods(page, size);
        for (DlGoods ds : findByIndexGoods) {
            String goodsId = ds.getId();
            ds.setPictureStr(ds.getPictureStr() + dlGoodsPicMapper.byGoodsIdFindPic(goodsId));
        }
        return dlGoodsMapper.findByIndexGoods(page, size);
    }

    @Override
    public GoodsDetail byIdFindGoods(String goodsId) throws Exception {
        GoodsDetail detail = new GoodsDetail();
        DlGoods selectByPrimaryKey = dlGoodsMapper.selectByPrimaryKey(goodsId);// 商品
        DlShop byGoodsIdFindShop = dlShopMapper.byGoodsIdFindShop(goodsId);
        List<DlShopClause> byAdminIdFindClause = dlShopClauseMapper.byAdminIdFindClause(numberDictionary.getClauseTxt(), byGoodsIdFindShop.getAdminId());
        for (DlShopClause clause : byAdminIdFindClause) {
            clause.setClauseRemark(tsTypeMapper.byIdAndTypeFind(numberDictionary.getClauseSt(), clause.getClause()).getTypename());
        }
        // Double byGoodsIdFindSum = dlOrderMapper.byGoodsIdFindSum(goodsId);
        List<DlOrderEvaluate> findAllEvaluate = dlOrderEvaluateMapper.findAllEvaluate(goodsId);
        Double findByGoodsEvaluate = dlOrderEvaluateMapper.findByGoodsEvaluate(goodsId);
        Double findByDescribeScoreScoreGoodsEvaluate = dlOrderEvaluateMapper
                .findByDescribeScoreScoreGoodsEvaluate(goodsId);
        Double findByQualityScoreGoodsEvaluate = dlOrderEvaluateMapper.findByQualityScoreGoodsEvaluate(goodsId);
        selectByPrimaryKey.setScore(findByGoodsEvaluate);
        selectByPrimaryKey.setDescribeScore(findByDescribeScoreScoreGoodsEvaluate);
        selectByPrimaryKey.setQualityScore(findByQualityScoreGoodsEvaluate);
        selectByPrimaryKey.setSalesVolume(dlOrderMapper.byGoodsIdFindSum(goodsId));

        List<DlGoodsPic> byGoodsIdFindAllPic = dlGoodsPicMapper.byGoodsIdFindAllPic(goodsId);
        List<String> byGoodsIdFindSsGg = dlGoodsSsMapper.byGoodsIdFindSsGg(goodsId);
        List<GoodsSsGg> ssgList = new ArrayList<>();
        for (String sSsName : byGoodsIdFindSsGg) {
            GoodsSsGg sSs = new GoodsSsGg();
            List<DlGoodsSs> findAllGoodsSs = dlGoodsSsMapper.findAllGoodsSs(sSsName);
            sSs.setSsagList(findAllGoodsSs);
            sSs.setSsGg(sSsName);
            for (DlGoodsSs dsss : findAllGoodsSs) {
                dsss.setSssPic(picURLEntity.getUrlPrefix() + dsss.getSssPic());
            }
            ssgList.add(sSs);
        }
        for (DlGoodsPic dp : byGoodsIdFindAllPic) {
            dp.setGoodsPicture(picURLEntity.getUrlPrefix() + dp.getGoodsPicture());
        }
        detail.setClauseList(byAdminIdFindClause);
        detail.setPicList(byGoodsIdFindAllPic);
        detail.setAdminId(byGoodsIdFindShop.getAdminId());
        detail.setShopName(byGoodsIdFindShop.getShopName());
        detail.setShopHead(picURLEntity.getUrlPrefix() + byGoodsIdFindShop.getShopPictrue());
        detail.setGoods(selectByPrimaryKey);
        detail.setSsgList(ssgList);
        detail.setOrderEvaluateList(findAllEvaluate);
        // detail.setScore(findByGoodsEvaluate);
        return detail;
    }

    @Override
    public List<DlGoods> byNameFindGoodsList(String name) throws Exception {
        JSONObject jsonObject = new JSONObject(name);
        int type = jsonObject.getInt("type");
        int page = jsonObject.getInt("page");
        int size = jsonObject.getInt("size");
        Double bPrice = jsonObject.getDouble("bPrice");
        Double ePrice = jsonObject.getDouble("ePrice");
        if (page > 0) {
            page = (page - 1) * 10;
        }
        jsonObject.getString("name");
        List<DlGoods> list = null;
        switch (type) {//综合
            case 1:
                list = dlGoodsMapper.likeGoodsList(name, page, size);
                break;
            case 2:
                list = dlGoodsMapper.orderDescList(name, page, size);
                break;
            case 3:
                list = dlGoodsMapper.byBPriceOrEPriceFindList(name, bPrice, ePrice, page
                        , size);
                break;
        }
        for (DlGoods dl : list) {
            String goodsId = dl.getId();
            dl.setPictureStr(picURLEntity.getUrlPrefix() + dl.getPictureStr());
            dl.setScore(dlOrderEvaluateMapper.findByGoodsEvaluate(goodsId));
            dl.setDescribeScore(dlOrderEvaluateMapper.findByDescribeScoreScoreGoodsEvaluate(goodsId));
            dl.setQualityScore(dlOrderEvaluateMapper.findByQualityScoreGoodsEvaluate(goodsId));
            dl.setSalesVolume(dlOrderMapper.byGoodsIdFindSum(goodsId));
        }


        return list;
    }

}
